﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using SharePointContentGenerator.ContentGeneratorLogic;
using SharePointContentGenerator.ContentGeneratorLogic.DataObjects;
using SharePointContentGenerator.ContentGeneratorLogic.Core;
using Microsoft.SharePoint;
using System.Security.Principal;

namespace FlickrTestHarness
{
    public partial class MainForm : Form
    {
        private List<Picture> pictures;
        private int currentIndex = 0;

        public MainForm()
        {
            InitializeComponent();
        }

        #region Download and View Pictures

        private void btnDownload_Click(object sender, EventArgs e)
        {
            Cursor = Cursors.WaitCursor;
            gbSearchOptions.Enabled = false;
            try
            {
                ContentGeneratorFacade facade = new ContentGeneratorFacade();
                facade.ContentGenerationStarted += download_ContentGenerationStarted;
                facade.ContentGenerationFinished += download_ContentGenerationFinished;
                facade.ContentGenerationChanging += download_ContentGenerationChanging;

                pictures = facade.GetRandomPictures(txtSearchOptionsTags.Text, (int)cbSearchOptionsNumImages.Value);

                DisplayImage(0);
            }
            finally
            {
                Cursor = Cursors.Default;
                gbSearchOptions.Enabled = true;
            }
        }

        private void DisplayImage(int index)
        {
            if (pictures != null && pictures.Count > 0)
            {
                pbThumbnails.Visible = true;
                btnPrevImage.Visible = true;
                btnNextImage.Visible = true;
                lblCurrentImage.Visible = true;
                lblImageName.Visible = true;

                Picture current = pictures[index];
                pbThumbnails.Image = current.Thumbnail;
                pbPhotos.Image = current.Image;
                btnPrevImage.Enabled = index > 0;
                btnNextImage.Enabled = index < pictures.Count - 1;

                lblImageName.Text = current.Title;
                lblCurrentImage.Text = string.Format("{0} of {1}", index + 1, pictures.Count);
                currentIndex = index;
            }
        }

        private void btnPrevImage_Click(object sender, EventArgs e)
        {
            DisplayImage(currentIndex - 1);
        }

        private void btnNextImage_Click(object sender, EventArgs e)
        {
            DisplayImage(currentIndex + 1);
        }

        void download_ContentGenerationChanging(object sender, ContentGeneratorItemEventArgs e)
        {
            ProgressBar.Value = e.CurrentItemIndex + 1;
            lblDownloadStatus.Text = string.Format("Downloading {0} of {1}", ProgressBar.Value, ProgressBar.Maximum);
            this.Refresh();
        }

        void download_ContentGenerationFinished(object sender, ContentGeneratorItemEventArgs e)
        {
            ProgressBar.Visible = false;
            lblDownloadStatus.Visible = false;
            this.Refresh();
        }

        void download_ContentGenerationStarted(object sender, ContentGeneratorItemEventArgs e)
        {
            ProgressBar.Maximum = e.TotalItems;
            ProgressBar.Minimum = 0;
            ProgressBar.Value = 0;
            lblDownloadStatus.Text = string.Format("Downloading {0} of {1}", ProgressBar.Value, ProgressBar.Maximum);
            ProgressBar.Visible = true;
            lblDownloadStatus.Visible = true;
            this.Refresh();
        }

        #endregion Download and View Pictures

        #region Create Picture Libraries

        private void btnSPCreatePictureLibrary_Click(object sender, EventArgs e)
        {
            Cursor = Cursors.WaitCursor;
            gbSearchOptions.Enabled = false;
            try
            {
                lvPictureLibraries.Items.Clear();

                ContentGeneratorFacade facade = new ContentGeneratorFacade();
                facade.ContentGenerationStarted += facade_ContentGenerationStarted;
                facade.ContentGenerationFinished += facade_ContentGenerationFinished;
                facade.ContentGenerationChanging += facade_ContentGenerationChanging;

                List<string> libraries = facade.CreatePictureLibraries(txtSPWebName.Text,
                    txtBasePictureLibraryName.Text,
                    (int)cbNumPictureLibraries.Value,
                    chkDeleteIfExists.Checked,
                    chkAddImages.Checked,
                    txtRandomWords.Text,
                    (int)cbMinNumImages.Value,
                    (int)cbMaxNumImages.Value);

                foreach (string library in libraries)
                {
                    lvPictureLibraries.Items.Add(new ListViewItem(library));
                }
            }
            finally
            {
                Cursor = Cursors.Default;
                gbSearchOptions.Enabled = true;
            }
        }

        void facade_ContentGenerationChanging(object sender, ContentGeneratorItemEventArgs e)
        {
            PictureLibraryProgressBar.Value = e.CurrentItemIndex + 1;
            lblPictureLibraryCreationStatus.Text = string.Format("Creating {0} of {1}", PictureLibraryProgressBar.Value, PictureLibraryProgressBar.Maximum);
            this.Refresh();
        }

        void facade_ContentGenerationFinished(object sender, ContentGeneratorItemEventArgs e)
        {
            PictureLibraryProgressBar.Visible = false;
            lblPictureLibraryCreationStatus.Visible = false;
            this.Refresh();
        }

        void facade_ContentGenerationStarted(object sender, ContentGeneratorItemEventArgs e)
        {
            PictureLibraryProgressBar.Maximum = e.TotalItems;
            PictureLibraryProgressBar.Minimum = 0;
            PictureLibraryProgressBar.Value = 0;
            lblPictureLibraryCreationStatus.Text = string.Format("Creating {0} of {1}", PictureLibraryProgressBar.Value, PictureLibraryProgressBar.Maximum);
            PictureLibraryProgressBar.Visible = true;
            lblPictureLibraryCreationStatus.Visible = true;
            this.Refresh();
        }

        private void chkAddImages_CheckedChanged(object sender, EventArgs e)
        {
            lblRandomWords.Enabled = chkAddImages.Checked;
            txtRandomWords.Enabled = chkAddImages.Checked;
            lblMinNumImages.Enabled = chkAddImages.Checked;
            cbMinNumImages.Enabled = chkAddImages.Checked;
            lblMaxNumImages.Enabled = chkAddImages.Checked;
            cbMaxNumImages.Enabled = chkAddImages.Checked;
        }

        #endregion Create Picture Libraries

        #region Delete Libraries

        private void btnDeleteLibrariesLoad_Click(object sender, EventArgs e)
        {
            Cursor = Cursors.WaitCursor;
            gbDeleteLibrariesOptions.Enabled = false;
            try
            {
                lbDeleteLibrariesList.Items.Clear();

                ContentGeneratorFacade facade = new ContentGeneratorFacade();

                int templateId = 101;
                switch (cbDeleteLibrariesTemplates.SelectedIndex)
                {
                    case 0:
                        templateId = 101;
                        break;
                    case 1:
                        templateId = 109;
                        break;
                    default:
                        templateId = 101;
                        break;
                }

                List<string> libraries = facade.LoadLibraries(txtDeleteLibrariesSite.Text, templateId);

                foreach (string library in libraries)
                {
                    lbDeleteLibrariesList.Items.Add(library);
                }
            }
            finally
            {
                Cursor = Cursors.Default;
                gbDeleteLibrariesOptions.Enabled = true;
            }

        }

        #endregion Delete Libraries

        private void btnDeleteLibrariesDelete_Click(object sender, EventArgs e)
        {
            bool bContinue = false;
            if (lbDeleteLibrariesList.CheckedItems.Count > 0)
            {
                bContinue = MessageBox.Show("Are you sure you wish to delete the selected lists?", "Delete Selected Lists", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes;                    
            }

            if (bContinue)
            {
                Cursor = Cursors.WaitCursor;
                gbDeleteLibrariesOptions.Enabled = false;
                try
                {
                    ContentGeneratorFacade facade = new ContentGeneratorFacade();

                    List<string> libraries = new List<string>();
                    for (int i = 0; i < lbDeleteLibrariesList.Items.Count; i++)
                    {
                        if (lbDeleteLibrariesList.GetItemChecked(i))
                        {
                            libraries.Add(lbDeleteLibrariesList.Items[i].ToString());
                        }
                    }

                    facade.DeleteLibraries(txtDeleteLibrariesSite.Text, libraries);

                    lbDeleteLibrariesList.Items.Clear();

                    int templateId = 101;
                    switch (cbDeleteLibrariesTemplates.SelectedIndex)
                    {
                        case 0:
                            templateId = 101;
                            break;
                        case 1:
                            templateId = 109;
                            break;
                        default:
                            templateId = 101;
                            break;
                    }

                    libraries = facade.LoadLibraries(txtDeleteLibrariesSite.Text, templateId);

                    foreach (string library in libraries)
                    {
                        lbDeleteLibrariesList.Items.Add(library);
                    }
                }
                finally
                {
                    Cursor = Cursors.Default;
                    gbDeleteLibrariesOptions.Enabled = true;
                }
            }
        }
    }
}
